/*
 * Created on 2006-3-4
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ah.html;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * 读取html中需要的内容
 * 
 * @author Administrator
 * 
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class GetHtml {
	private static long star = 0;

	private static long end = 0;

	private static long ttime = 0;

	// 返回html代码
	public static String getHtmlCode(String httpUrl) {
		/*
		 * Date before = new Date(); star = before.getTime();
		 */
		StringBuffer htmlCode = new StringBuffer();
		try {
			InputStream in;
			URL url = new java.net.URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/4.0");
			connection.connect();
			in = connection.getInputStream();
			java.io.BufferedReader breader = new BufferedReader(
					new InputStreamReader(in, "GBK"));
			String currentLine;
			while ((currentLine = breader.readLine()) != null) {
				htmlCode.append(currentLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			/*
			 * Date after = new Date(); end = after.getTime(); ttime = end -
			 * star; System.out.println("执行时间:" + ttime + "秒");
			 */
		}
		return htmlCode.toString();
	}

	// 存储文件
	public static synchronized void writeHtml(String filePath, String info,
			String flag) {
		PrintWriter pw = null;
		File writeFile = null;
		try {
			writeFile = new File(filePath);
			boolean isExit = writeFile.exists();

			if (isExit != true) {
				File dir = writeFile.getParentFile();
				if (!dir.exists()) {
					dir.mkdirs();
				}
				writeFile.createNewFile();
			} else {
				if (!flag.equals("NO")) {
					writeFile.delete();
					writeFile.createNewFile();
				}
			}

			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					filePath, false), "GBK"));
			pw.println(info);
			pw.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			pw.close();
		}
	}

	public static void main(String[] args) {

		/*
		 * 读取a-z的网页
		 */
		String urlPart = "http://jbk.39.net/py/";
		String urlPart2 = ".html";

		char c = 'a';
		for (int i = 0; i < 26; i++) {
			String url = "http://jbk.39.net/py/" + c + ".html";
			writeHtml("D:/39jk/" + c + ".html", getHtmlCode(url), "NO");
			c++;
			try {
				Thread.sleep(1000 * 30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		/*
		 * 
		 */
	}
}
